package com.briup.web;

import com.briup.pojo.Book;
import com.briup.utils.Result;
import com.briup.utils.ResultCode;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {

  // 映射配置
  // http://127.0.0.1:8081/books/getById/1
  // @RequestMapping(value="/getById/{id}",method = RequestMethod.GET)
  // 简化的配置
  // @GetMapping("/getById/{id}")
  @GetMapping("/{id}") // http://127.0.0.1:8081/books/2
  public Result getById(@PathVariable Integer id) {
    System.out.println("getById" + id);
    Book book = new Book();
    book.setId(id);
    book.setName("Java核心讲义");
    // 生成一个异常
    int n = id / 0;

    // 无统一标准的返回
    // return book;
    // 模拟失败的请求
    if (id == 1){
      return Result.failure(ResultCode.DATA_NONE);
    }
    return Result.success(book);
    // {
    // code:200,
    // messsage:"success,书籍查询成功",
    // data:book,
    // }
  }

  // http://127.0.0.1:8081/books/delById/1
  // @RequestMapping(value="/delById/{id}",method = RequestMethod.DELETE)
  // 简化的配置
  // @DeleteMapping("/delById/{id}")
  @DeleteMapping("/{id}") // http://127.0.0.1:8081/books/2
  public Result delById(@PathVariable Integer id) {
    System.out.println("delById" + id);
    // Book book = new Book();
    // book.setId(id);
    // book.setName("Java核心讲义");
    // return "book" + id + "删除成功";
    return Result.success();
    // {
    // code:200,
    // messsage:"success,书籍删除成功",
    // data:null,
    // }

  }

}